package com.cnswhy.cloud.server.core.utils.BaiduAip;

import com.alibaba.fastjson.JSON;
import com.cnswhy.cloud.server.core.entity.IdCardResults;
import lombok.extern.slf4j.Slf4j;
import com.baidu.aip.util.Base64Util;

import java.net.URLEncoder;

/**
 * ClaseName：Idcard
 * Description：
 * Author：JensenWei
 * QQ: 2188307188
 * Createtime：2018/3/10 11:03
 * Modified By：
 * Fixtime：2018/3/10 11:03
 * FixDescription：
 */
@Slf4j
public class Idcard {

    /**
     * 重要提示代码中所需工具类
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 下载
     */
    public static void main(String[] args) {
        // 身份证识别url
        String idcardIdentificate = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
        // 本地图片路径
        String filePath = "http://121.201.20.215/group1/M00/0D/25/wKhkcltYMQaATqe9AADgXuYdfGk604.png";
        try {
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            // 识别身份证正面id_card_side=front;识别身份证背面id_card_side=back;
            String params = "id_card_side=front&detect_risk=true&detect_direction=true&" + URLEncoder.encode("image", "UTF-8") + "="
                    + URLEncoder.encode(imgStr, "UTF-8");
            /**
             * 线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
             */
            String accessToken = AuthService.getAuth();
            String result = HttpUtil.post(idcardIdentificate, accessToken, params);
            if (result != null) {
                result = result.replace("住址", "address").replace("公民身份号码", "idNo").replace("出生", "birthDay")
                        .replace("姓名", "name").replace("民族", "nation").replace("性别", "sex").replace("签发日期", "effectDate").replace("失效日期", "invalidDate")
                        .replace("签发机关", "agency");
            }
            System.out.println(result);
            IdCardResults idCardResults = new IdCardResults();
            idCardResults = JSON.parseObject(result, IdCardResults.class);
            System.out.println(idCardResults.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static IdCardResults identify(String path) {
        // 身份证识别url
        String idcardIdentificate = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
        // 本地图片路径
        String filePath = path;
        try {
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            // 识别身份证正面id_card_side=front;识别身份证背面id_card_side=back;
            String params = "id_card_side=front&detect_risk=true&detect_direction=true&" + URLEncoder.encode("image", "UTF-8") + "="
                    + URLEncoder.encode(imgStr, "UTF-8");
            /**
             * 线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
             */
            String accessToken = AuthService.getAuth();
            String result = HttpUtil.post(idcardIdentificate, accessToken, params);
            if (result != null) {
                result = result.replace("住址", "address").replace("公民身份号码", "idNo").replace("出生", "birthDay")
                        .replace("姓名", "name").replace("民族", "nation").replace("性别", "sex");
            }
            log.info(result);
            IdCardResults idCardResults = new IdCardResults();
            idCardResults = JSON.parseObject(result, IdCardResults.class);
            return idCardResults;
        } catch (Exception e) {
            e.printStackTrace();
            log.info("识别失败：{}", e.toString());
            return null;
        }
    }

    public static IdCardResults identifyBack(String path) {
        // 身份证识别url
        String idcardIdentificate = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
        // 本地图片路径
        String filePath = path;
        try {
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            // 识别身份证正面id_card_side=front;识别身份证背面id_card_side=back;
            String params = "id_card_side=back&detect_risk=true&detect_direction=true&" + URLEncoder.encode("image", "UTF-8") + "="
                    + URLEncoder.encode(imgStr, "UTF-8");
            /**
             * 线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
             */
            String accessToken = AuthService.getAuth();
            String result = HttpUtil.post(idcardIdentificate, accessToken, params);
            if (result != null) {
                result = result.replace("签发日期", "effectDate").replace("失效日期", "invalidDate")
                        .replace("签发机关", "agency");
            }
            log.info(result);
            IdCardResults idCardResults = new IdCardResults();
            idCardResults = JSON.parseObject(result, IdCardResults.class);
            return idCardResults;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
